﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PrintAgentService;

namespace PrintingManagerSystem
{
    public partial class PrinterSetting : Form
    {
        public PrinterSetting()
        {
            InitializeComponent();
            BindingSysPrinterList();
            BindingAppPrinterList();
        }

        void BindingSysPrinterList()
        {
            this.lbxPrinterList.Items.Clear();
            var list = PrintAgent.PrinterArray;
            foreach (var p in list)
            {
                this.lbxPrinterList.Items.Add(p);
            }
        }

        void BindingAppPrinterList()
        {
            this.lvAppPrinter.Items.Clear();
            for (int i = 0; i < Properties.Settings.Default.PrinterList.Count; i++)
            {
                ListViewItem newP = new ListViewItem();
                newP.Text = i.ToString();
                newP.SubItems.Add(Properties.Settings.Default.PrinterList[i]);
                this.lvAppPrinter.Items.Add(newP);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.lbxPrinterList.SelectedItem != null)
            {
                Properties.Settings.Default.PrinterList.Add(this.lbxPrinterList.SelectedItem.ToString());
                Properties.Settings.Default.Save();
                BindingAppPrinterList();
            }
            else
            {
                MessageBox.Show("请选择要移入的打印机");
            }
        }

        private void btnUnSelect_Click(object sender, EventArgs e)
        {
            if (this.lvAppPrinter.SelectedItems.Count > 0)
            {
                if (Properties.Settings.Default.PrinterList.Count > this.lvAppPrinter.SelectedItems[0].Index)
                {
                    Properties.Settings.Default.PrinterList.RemoveAt(this.lvAppPrinter.SelectedItems[0].Index);
                    Properties.Settings.Default.Save();
                    BindingAppPrinterList();
                }
            }
            else
            {
                MessageBox.Show("请选择要移除的打印机");
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (this.lvAppPrinter.SelectedItems.Count > 0)
            {
                if (this.lvAppPrinter.SelectedItems[0].Index > 0)
                {
                    if (Properties.Settings.Default.PrinterList.Count > this.lvAppPrinter.SelectedItems[0].Index)
                    {
                        int priorIndex = this.lvAppPrinter.SelectedItems[0].Index - 1;
                        string temp = Properties.Settings.Default.PrinterList[priorIndex];
                        Properties.Settings.Default.PrinterList[priorIndex] = Properties.Settings.Default.PrinterList[this.lvAppPrinter.SelectedItems[0].Index];
                        Properties.Settings.Default.PrinterList[this.lvAppPrinter.SelectedItems[0].Index] = temp;
                        Properties.Settings.Default.Save();
                        BindingAppPrinterList();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要上移的打印机");
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (this.lvAppPrinter.SelectedItems.Count > 0)
            {
                if (this.lvAppPrinter.SelectedItems[0].Index < this.lvAppPrinter.Items.Count - 1)
                {
                    if (Properties.Settings.Default.PrinterList.Count > this.lvAppPrinter.SelectedItems[0].Index + 1)
                    {
                        int nextIndex = this.lvAppPrinter.SelectedItems[0].Index + 1;
                        string temp = Properties.Settings.Default.PrinterList[nextIndex];
                        Properties.Settings.Default.PrinterList[nextIndex] = Properties.Settings.Default.PrinterList[this.lvAppPrinter.SelectedItems[0].Index];
                        Properties.Settings.Default.PrinterList[this.lvAppPrinter.SelectedItems[0].Index] = temp;
                        Properties.Settings.Default.Save();
                        BindingAppPrinterList();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要上移的打印机");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindingSysPrinterList();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PrinterSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
            List<string> printerNames = new List<string>();
            foreach (var item in Properties.Settings.Default.PrinterList)
            {
                printerNames.Add(item);
            }
            CPrintStatus.RefreshPrinter(printerNames);
        }
    }
}
